package shun.demo.remote.display.model;

import android.animation.ObjectAnimator;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.*;
import shun.demo.remote.display.client.XScreen;

import java.io.Serializable;
import java.net.Socket;

/**
 * Created by chen on 14-5-21.
 */
public class Movement implements Serializable {
    private static final long serialVersionUID = 5L;

    public static final int DECELERATE_INTERPOLATOR = 1;
    private float mStartX;
    private float mStartY;
    private float mFinalX;
    private float mFinalY;
    private long mDuration;
    private int mInterpolator;

    private long mTime;
    private transient Socket mFrom;

    public Movement(float startX, float finalX, float startY, float finalY, long duration) {
        this(startX, finalX, startY, finalY, duration, 0);
    }

    public Movement(float startX, float finalX, float startY, float finalY, long duration, int interpolator) {
        mStartX = startX;
        mFinalX = finalX;
        mStartY = startY;
        mFinalY = finalY;
        mDuration = duration;
        mInterpolator = interpolator;
        mTime = SystemClock.uptimeMillis() + XScreen.get().getTimeOffset();
    }

    public float getStartX() {
        return mStartX;
    }

    public void setStartX(float startX) {
        this.mStartX = startX;
    }

    public float getStartY() {
        return mStartY;
    }

    public void setStartY(float startY) {
        this.mStartY = startY;
    }

    public float getFinalX() {
        return mFinalX;
    }

    public void setFinalX(float finalX) {
        this.mFinalX = finalX;
    }

    public float getFinalY() {
        return mFinalY;
    }

    public void setFinalY(float finalY) {
        this.mFinalY = finalY;
    }

    public long getDuration() {
        return mDuration;
    }

    public void setDuration(long duration) {
        mDuration = duration;
    }

    public long getTime() {
        return mTime;
    }

    public void setTime(long time) {
        mTime = time;
    }

    public Socket getFrom() {
        return mFrom;
    }

    public void setFrom(Socket from) {
        mFrom = from;
    }

    @Override
    public String toString() {
        return "Movement{" +
                "mStartX=" + mStartX +
                ", mStartY=" + mStartY +
                ", mFinalX=" + mFinalX +
                ", mFinalY=" + mFinalY +
                ", mTime=" + mTime +
                ", mFrom=" + mFrom +
                '}';
    }

    public ObjectAnimator createAnimatorXFor(View target) {
        ObjectAnimator a = ObjectAnimator.ofFloat(target, "x", mStartX, mFinalX);
        a.setDuration(mDuration);
        a.setInterpolator(getInterpolator());
        return a;
    }

    public ObjectAnimator createAnimatorYFor(View target) {
        ObjectAnimator a = ObjectAnimator.ofFloat(target, "y", mStartY, mFinalY);
        a.setDuration(mDuration);
        a.setInterpolator(getInterpolator());
        return a;
    }

    private Interpolator getInterpolator() {
        switch (mInterpolator) {
            case DECELERATE_INTERPOLATOR:
                return new DecelerateInterpolator();
            default:
                return new LinearInterpolator();
        }
    }

}
